<template>
  <div v-loading="loading" class="employees-detail-container">
    <div v-if="!loading" class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账号设置">
            <!-- 放置表单 -->
            <el-form ref="userForm" label-width="120px" style="margin-left: 120px; margin-top:30px" :model="userInfo" :rules="rules">
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="userInfo.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="密码:">
                <el-input v-model="newPassword" style="width:300px" type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="submit">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <!-- 内容 -->
            <UserInfo :form-data="userInfo" @submit="submitUserInfo" />
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <!-- 内容 -->
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import UserInfo from './components/UserInfo'
import { getUserInfoApi, saveUserInfoAPI } from '@/api'
export default {
  name: 'EmployeesDetail',
  components: {
    UserInfo
  },
  data() {
    return {
      loading: false, // 控制显示隐藏/加载
      // 表单
      userInfo: {
        username: ''
      },
      newPassword: '',
      rules: {
        username: [
          { required: true, message: '姓名不能为空', trigger: ['blur', 'change'] }
        ],
        newPassword: [
          { min: 6, max: 9, message: '密码必须是6-9位', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  // 获取传过来的id 计算属性要return
  computed: {
    userId() {
      return this.$route.params.id
    }
  },
  created() {
    // console.log(this.$route)
    this.getUserInfo()
  },
  methods: {
    // 获取用户信息
    async getUserInfo() {
      this.loading = true
      const { data } = await getUserInfoApi(this.userId)
      this.userInfo = data
      // console.log(this.userInfo)
      this.loading = false
    },
    // 更新用户信息
    submit() {
      // 兜底验证
      this.$refs.userForm.validate(async(valid) => {
        if (valid) {
          const res = await saveUserInfoAPI(this.userInfo, ...{ password: this.newPassword })
          this.$message.success('用户名密码修改成功')
          console.log(res)
        } else {
          return false
        }
      })
    },
    submitUserInfo() {

    }

  }
}
</script>

<style>

</style>
